//
//  ProgressView.m
//  之定义下载进度条2
//
//  Created by Jorn.Wu on 15/6/28.
//  Copyright (c) 2015年 Jorn.Wu. All rights reserved.
//

#import "ProgressView.h"

#define JWColorWithRGB(r, g, b, a) [UIColor colorWithRed:r / 255.f green:g / 255.f blue:b / 255.f alpha:a]
@interface ProgressView ()

@property (nonatomic, assign) CGFloat R;
@property (nonatomic, assign) CGFloat G;
@property (nonatomic, assign) CGFloat B;

@end

@implementation ProgressView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = frame;
    }
    return self;
}
- (UILabel *)label{
    if (_label == nil) {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
        label.textColor = [UIColor redColor];
        label.center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height /2);
        label.font = [UIFont systemFontOfSize:50];
        label.textAlignment = UITextAlignmentCenter;
        _label = label;
        [self addSubview:_label];
    }
    return _label;
}
- (void)setProgress:(CGFloat)progress{
    _progress = progress;
    self.label.text = [NSString stringWithFormat:@"%.2f%%",_progress * 100];
    [self setNeedsDisplay];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    [self drawProgress];
}
- (void)drawProgress{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGPoint center = CGPointMake(self.bounds.origin.x + self.bounds.size.width / 2, self.bounds.origin.y + self.bounds.size.height / 2);
    CGFloat radius = self.frame.size.width / 2 - 5;
    CGFloat startA = -M_PI_2;
    CGFloat endA = startA + _progress * M_PI * 2;
    CGContextSetLineWidth(context, 10);
    
    if (_R >= 255) {
        _R = 0;
    }else if (_G >= 255){
        _G = 0;
    }else if (_B >= 255){
        _B = 0;
    }
    _R = _R + 2;
    _G = _G + 3;
    _B = _B + 1;
    
    [JWColorWithRGB(_R, _G, _B, 1) set];
    
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
    [path addLineToPoint:center];
    //    CGContextAddArc(context, center.x, center.y, radius, startA, endA, YES);
    CGContextAddPath(context, path.CGPath);
    CGContextFillPath(context);
    
}


@end
